package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.annotation.ApiDoc;
import cn.chinaflame.pagodas.constant.Sysconst;
import cn.chinaflame.pagodas.dto.AreaDto;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.req.AreaReq;
import cn.chinaflame.pagodas.service.YwAreaService;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.entity.Boy;
import cn.chinaflame.pagodas.repository.BoyRepository;
import cn.chinaflame.pagodas.service.BoyService;
import cn.chinaflame.pagodas.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
/**
 * 区域接口
 *
 * @author created by boming
 * @date 2018年1月5日 上午9:45:37
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/openapi/v1/pagodas/area/")
public class OpenAreaAPI {

    private static final Logger log = LoggerFactory.getLogger(OpenAreaAPI.class);

    @Autowired
    private YwAreaService ywAreaService;

    /**
     * 查询区域列表
     *
     * @param req
     * @return
     */
    @PostMapping("query/list")
    @ApiDoc(value = AreaReq.class, result = AreaDto.class)
    public Object queryList(@RequestBody AreaReq req) {
        log.info("查询区域列表req:{}", req);
        List<AreaDto> dtoList = ywAreaService.queryList(req.getCode(), req.getParentCode(), req.getType());
        return Result.create(dtoList);
    }

    /**
     * 查询区域
     *
     * @param code 区域编码
     * @return
     */
    @GetMapping("query/{code}")
    @ApiDoc(AreaDto.class)
    public Object query(@PathVariable String code) {
        log.info("查询单个区域code:{}", code);
        ParamPreconditions.checkNotEmpty(code, CodeEnum.FAIL.getCode(), "code不能为空");
        List<AreaDto> dtoList = ywAreaService.queryList(code, null, null);
        ParamPreconditions.checkArgument(dtoList != null && dtoList.size() > 0 && dtoList.get(0) != null, CodeEnum.FAIL.getCode(), "未找到区域信息");
        return Result.create(dtoList.get(0));
    }

    /**
     * 查询区域关联列表
     *
     * @return
     */
    @GetMapping("query/leftlist")
    @ApiDoc(Result.class)
    public Object queryLeftList() {
        log.info("查询区域关联列表");

        return Result.create(ywAreaService.queryLeftList());
    }

    /**
     * 添加区域（开发使用）
     *
     * @param code       区域编码
     * @param name       区域名称
     * @param parentCode 父级编码
     * @param type       区域类型
     * @return
     */
    @PostMapping(value = "add")
    @ApiDoc(AreaDto.class)
    public Object add(String code, String name, String parentCode, String type) {
        log.info("添加单个区域code:{},name:{},parentCode:{},type:{}", code, name, parentCode, type);
        ParamPreconditions.checkNotEmpty(CodeEnum.FAIL.getCode(), "必要参数为空", "必要参数为空",
                code, name, type);
        ParamPreconditions.checkArgument(Sysconst.AreaTypeList.containsKey(type), CodeEnum.FAIL.getCode(), "区域类型不支持");
        ParamPreconditions.checkArgument(!type.equals(Sysconst.AREA_TYPE_PROVINCE) && !Strings.isNullOrEmpty(parentCode),
                CodeEnum.FAIL.getCode(), "市、区级别父区域编码不能为空");
        AreaDto dto = ywAreaService.add(code, name, parentCode, type);
        return Result.create(dto);
    }

    /**
     * 更新区域（开发使用）
     *
     * @param code       区域编码
     * @param name       区域名称
     * @param parentCode 父级编码
     * @param type       区域类型
     * @return
     */
    @PutMapping("update")
    @ApiDoc(AreaDto.class)
    public Object update(String code, String name, String parentCode, String type) {
        log.info("更新单个区域code:{},name:{},parentCode:{},type:{}", code, name, parentCode, type);
        ParamPreconditions.checkNotEmpty(CodeEnum.FAIL.getCode(), "必要参数为空", "必要参数为空",
                code, name, type);
        ParamPreconditions.checkArgument(Sysconst.AreaTypeList.containsKey(type), CodeEnum.FAIL.getCode(), "区域类型不支持");
        ParamPreconditions.checkArgument(!type.equals(Sysconst.AREA_TYPE_PROVINCE) && !Strings.isNullOrEmpty(parentCode),
                CodeEnum.FAIL.getCode(), "市、区级别父区域编码不能为空");
        AreaDto dto = ywAreaService.update(code, name, parentCode, type);
        return Result.create(dto);
    }

    /**
     * 删除区域信息（开发使用）
     *
     * @param code 编码
     * @return
     */
    @DeleteMapping("delete")
    @ApiDoc(Result.class)
    public Object delete(@PathVariable("id") String code) {
        log.info("删除单个区域code:{}", code);
        Boolean b = ywAreaService.delete(code);
        return Result.create(b);
    }
}
